import Vue from 'vue';
import Vuex from 'vuex';
import VuexPersistence from 'vuex-persist';
import httptool from '@/js/plugins/zry.plugins.httptool.js';
import bus from '@/js/common/bus.js';
import dictionary from './dictionary.js';
import userinfo from './userinfo.js';
import mark from './mark.js';
import order from './order.js';
import aboutus from './aboutus.js';
import map_ from './map.js';
Vue.use(Vuex);
const vuexLocal = new VuexPersistence({
	storage: window.sessionStorage
})
export const store = new Vuex.Store({
	state: {
		...dictionary,
		...userinfo,
		...mark,
		...order,
		...aboutus,
		...map_,
		categoryTree:[] , //头部分类
		BMap:null
	},
	getters: {
		changeState: state => {
			//相当于vue实例中的methods,用于处理公用data
			//自vuex 面向组件的数据处理
		}
	},
	plugins: [vuexLocal.plugin],
	mutations: {
		changeValue(state, obj) {
			state[obj.name] = obj.value;
		},
		getDirByKey(state, key_) {
			return new Promise((resolve, reject) => {
				let obj = this.state[key_];
				if(Object.keys(obj).length != 0) {
					resolve(obj);
				} else {
					let params = {
						categorys: key_,
						pageNum: 1,
						pageSize: 100
					};
					httptool.ajaxPost("dict/getListValue", params, res => {
						res.arr_key = key_;
						bus.$emit('changeValue', res);
						resolve(res);
					})
				}
			})
		},
		resetStore(state){
			let arr = ['userId','userInfo','registParams'];
			for(let item of arr){
				
				if(typeof  state[item] == 'object'){
					if(Array.isArray(state[item])){
						state[item] = []
					}else{
						state[item] = {};
					}
				}else{
					state[item] =  null;
				}
			}
		}
		//写法与getters相类似
		//组件想要对于vuex 中的数据进行的处理
		//组件中采用this.$store.commit('方法名') 的方式调用，实现充分解耦
		//内部操作必须在此刻完成(同步)
	},
	actions: {
		//使得mutations能够实现异步调用，实现例如延迟调用
		increment({
			commit
		}, payload) {
			commit('突变方法名')
			//payload应该是一个对象，可通过模板方法调用传入对象的方式将数据从组件传入vuex
		},
	},
	modules: {
		//引入某一个state的以上集合的模块，会自动分别填充到上面，使得结构更加清晰
	}
});